package com.hz.ruoyiequipment.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.ruoyiequipment.equipment.domain.AgrEnvRegulation;
import com.hz.ruoyiequipment.equipment.domain.AgrEnvRegulationDetail;
import com.hz.ruoyiequipment.equipment.mapper.AgrEnvRegulationMapper;
import com.hz.ruoyiequipment.equipment.service.AgrEnvRegulationService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 高铭月
 * @date 2025/3/7 11:32
 * @description:
 */
@Service
public class AgrEnvRegulationServiceImpl
        extends ServiceImpl<AgrEnvRegulationMapper, AgrEnvRegulation>
        implements AgrEnvRegulationService {

    @Autowired
    private AgrEnvRegulationMapper agrEnvRegulationMapper;
    @Override
    public List<AgrEnvRegulation> getAgrEnvRegulation(AgrEnvRegulation agrEnvRegulation) {
        return agrEnvRegulationMapper.getAgrEnvRegulation(agrEnvRegulation);
    }

    @Override
    public AjaxResult getAgrEnvRegulationByEquipmentId(Long equipmentId) {
        return AjaxResult.success(
                agrEnvRegulationMapper.getAgrEnvRegulationByEquipmentId(equipmentId));
    }
    @Override
    public Integer getAgrEnvRegulationStatusByEquipmentIdInner(Long equipmentId) {
        return agrEnvRegulationMapper.getAgrEnvRegulationByEquipmentIdInner(equipmentId).getEquipmentStatus();
    }


    @Override
    public AjaxResult updateStatus(Long equipmentId, Integer status) {
        try {
            int result = baseMapper.updateStatus(equipmentId, status);
            if (result > 0) {
                return AjaxResult.success("状态更新成功");
            }
            return AjaxResult.error("设备不存在或状态未变化");
        } catch (Exception e) {
            log.error("设备状态更新失败", e);
            return AjaxResult.error("状态更新失败：" + e.getMessage());
        }

    }

    @Override
    public AjaxResult insertRegulationEquipment(AgrEnvRegulation regulation) {
        try {
            // 插入设备基础信息
            agrEnvRegulationMapper.insertAgrEquipment(regulation);
            // 插入环境调控信息
            agrEnvRegulationMapper.insertAgrEnvRegulation(regulation);
            // 关联地块
            if (regulation.getPlotsId() != null) {
                agrEnvRegulationMapper.insertAgrEquipmentPlots(regulation.getEquipmentId(), regulation.getPlotsId());
            }
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            log.error("新增失败", e);
            throw new ServiceException("新增失败: " + e.getMessage());
        }
    }

    @Override
    public AjaxResult updateRegulationEquipment(AgrEnvRegulation regulation) {
        try {
            // 更新设备基础信息
            agrEnvRegulationMapper.updateAgrEquipment(regulation);
            // 更新环境调控信息
            agrEnvRegulationMapper.updateAgrEnvRegulation(regulation);

            if (regulation.getPlotsId() != null && regulation.getPlotsId() != 0L) {
                // 删除原有地块关联并更新
                agrEnvRegulationMapper.deleteAgrEquipmentPlots(regulation.getEquipmentId());
                agrEnvRegulationMapper.insertAgrEquipmentPlots(regulation.getEquipmentId(), regulation.getPlotsId());
            }
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            log.error("修改失败", e);
            throw new ServiceException("修改失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delRegulationEquipment(Long equipmentId) {
        // 验证设备是否存在
        AgrEnvRegulationDetail equipment = agrEnvRegulationMapper.getAgrEnvRegulationByEquipmentId(equipmentId);
        if (equipment == null) {
            return AjaxResult.error("设备不存在或已被删除");
        }
        // 删除设备-地块关联
        agrEnvRegulationMapper.deleteAgrEquipmentPlots(equipmentId);
        // 删除环境调控信息
        agrEnvRegulationMapper.deleteAgrEnvRegulation(equipmentId);
        // 删除设备基础信息
        agrEnvRegulationMapper.deleteAgrEquipment(equipmentId);
        return AjaxResult.success("删除成功");
    }
}
